<?php

namespace App\Util;

class DataUtil
{
    /**
     * 合并数组字符串
     * @param $input array
     * @param $mergeElement mixed 合并的元素
     * @param $mergePosition int 合并的位置，默认是第一个
     * @param $separator string 分隔符
     *
     * @author Chris Yu <chrisyu@crabapple.top> 2023/6/6
     */
    public static function mergeListString($input, $mergeElement, $mergePosition = 0, $separator = ',')
    {
        // 分割
        $inputArray = [];
        if ($input) {
            $inputArray = explode($separator, $input);
        }
        // 插入
        array_splice($inputArray, $mergePosition, 0, $mergeElement);
        // 合并
        return implode($separator, $inputArray);
    }

    /**
     * 数组浮点转格式化字符串
     * @author Chris Yu <chrisyu@crabapple.top> 2023/12/29
     */
    public static function convertFloatToStringFormatInArray($input, $excludeField = [], $precision = 2, $sprintfFormat = '%.2f')
    {
        foreach ($input as $key => &$value) {
            if (in_array($key, $excludeField, true)) {
                continue;
            }
            if (is_array($value)) {
                $value = self::convertFloatToStringFormatInArray($value, $excludeField, $precision, $sprintfFormat);
            }
            elseif (is_float($value)) {
                $value = sprintf($sprintfFormat, round($value, $precision));
            }
        }
        unset($value);
        return $input;
    }

    /**
     * 浮点转字符串
     * @param $input float|mixed
     * @param $withDecimal bool 是否带小数
     * @param $sprintfFormat string 格式
     *
     * @author Chris Yu <chrisyu@crabapple.top> 2024/1/8
     */
    public static function convertFloatToString($input, $withDecimal = true, $sprintfFormat = '%.2f', $precision = 2)
    {
        if ($input == (int)$input && $withDecimal === false) {
            $output = sprintf('%d', $input);
        }
        else {
            $output = sprintf($sprintfFormat, round($input, $precision));
        }
        return $output;
    }

    /**
     * 数组过滤空值
     * @param callable $callback
     * @param          $data
     * @param          $exclude
     *
     * @return array
     * @author Chris Yu <chrisyu@crabapple.top> 2024/4/8
     */
    public static function arrayFilterEmpty($data, $exclude)
    {
        return array_filter($data, function ($item, $key) use ($exclude) {
            return ($item !== '' && $item !== null) || in_array($key, $exclude);
        }, ARRAY_FILTER_USE_BOTH);
    }

    public static function isEmpty($val)
    {
        return $val === '' || $val === null;
    }

    public static function isNotEmpty($val)
    {
        return !self::isEmpty($val);
    }

    /**
     * 数组根据字段排序
     * @param $array
     * @param $field
     * @param $ascending
     *
     * @author Chris Yu <chrisyu@crabapple.top> 2024/5/23
     */
    public static function sortArrayByField(&$array, $field, $asc = true)
    {
        $column = array_column($array, $field);
        $sortOrder = $asc ? SORT_ASC : SORT_DESC;
        array_multisort($column, $sortOrder, $array);
    }
}
